import { getUserInfoFn, getUserDetailById } from "../../apis/api";
let user: Object = {
    state: {
        loginState: localStorage.getItem('loginState') === 'true',
        mobile: localStorage.getItem('mobile') || '',
        // 设置token初始状态   token持久化 => 放到缓存中
        token: localStorage.getItem('token') || '',
        // 定义一个空的对象 不是null 因为后边要开发userInfo的属性给别人用  userInfo.name
        userInfo: JSON.parse(localStorage.getItem("userInfo") || "0")
    },
    getters: {
    },
    mutations: {
        CHANGE_LOGINSTATE(state: any, loginBool: any) {
            localStorage.setItem('loginState', loginBool)
            state.loginState = loginBool
        },
        CHANGE_MOBILE(state: any, Mobile: any) {
            localStorage.setItem('mobile', Mobile)
            state.mobile = Mobile
        },
        CHANGE_TONKEN(state: any, loginToken: any) {
            localStorage.setItem('token', loginToken)
            state.token = loginToken
        },
        SETUSERINFO(state: any, userInfo: any) {
            localStorage.setItem('userInfo', JSON.stringify(userInfo))
            // 用 浅拷贝的方式去赋值对象 因为这样数据更新之后，才会触发组件的更新
            state.userInfo = { ...userInfo }
        },
        // 删除用户信息
        REMOVEUSERINFO(state: any) {
            localStorage.removeItem("userInfo")
            state.userInfo = {}
        }
    },
    actions: {
        // 获取用户信息资料
        async getUserInfo(context: any) {
            // 获取的result就是用户的基本资料
            const result = await getUserInfoFn()
            console.log(result)
            // 为了获取头像
            const imgInfo = await getUserDetailById(result.data.userId)
            //将两个接口信息合并
            const baseResult = { ...result, ...imgInfo }
            // 将整个的个人信息设置到用户的vuex数据中
            context.commit('SETUSERINFO', baseResult)
            return baseResult
        }
    },
    modules: {
    }
}
export default user